<?php
/**
 * Created by PhpStorm.
 * User: j28981
 * Date: 2015/8/30
 * Time: 10:33
 */
class goodsModel extends dbo{
protected $table="goods";
    public function getAllGoods($offset,$length,$search_field='',$field_value='',$status=1){
        $where = " good_status =$status";
        if(!empty($search_field) && !empty($field_value)){
            $where .= " and g.$search_field='$field_value'";
        }
        $sql = "select * from {$this->getTable('goods')} g left join {$this->getTable('category')} c on g.cate_id=c.cate_id where $where";
        //echo $where;die;
        if(empty($search_field) || empty($field_value)){
            $sql .= " order by good_id desc limit $offset,$length";
        }
        return $this->queryAll($sql);
    }
    public function getGoodsById($goods_id){
        $sql = "select * from {$this->getTable('goods')} g left join {$this->getTable('category')} c on g.cate_id=c.cate_id where g.good_id='$goods_id' ";
        return $this->queryRow($sql);

    }
    public function getGoodsCount($status=1){
        $sql = "select count(*) as c from {$this->getTable()} where good_status ='$status'";
        $c_array = $this->queryRow($sql);
        return $c_array['c'];
    }
    public function addGoods($insert_array){
        $fields =$values= '';
        foreach($insert_array as $field=>$value){
            $fields.=$field.',';
            $values.="'$value',";
        }
        $fields = rtrim($fields,',');
        $values = rtrim($values,',');
        $sql = "insert into {$this->getTable()} ($fields) values ($values)";
        return $this->Insert($sql);
    }
    public function delteGoodsOne($goods_id){
        if(!$goods_id){
            $res['status']=false;
            $res['msg'] = "请选择商品";
        }else{
            $sql = "update {$this->getTable()} set good_status=0 where good_id ='$goods_id'";
            if($this->Update($sql)){
                $res['status']=true;
                $res['msg'] = "商品添加回至回收站，如想永久删除，请到回收站删除";
            }else{
                $res['status']=false;
                $res['msg'] = "系统原因";
            }
        }
        return $res;
    }
    public function delGoodsMore($goods_id_array){
        $res = array();
        if(empty($goods_id_array)){
            $res['status']=false;
            $res['msg'] = "请选择商品";
        }else{
            $goods_ids = implode(',',$goods_id_array);
            $sql = "update {$this->getTable()} set good_status=0 where good_id in ($goods_ids)";
            if($this->Update($sql)){
                $res['status']=true;
                $res['msg'] = "删除多个商品成功";
            }else{
                $res['status']=false;
                $res['msg'] = "删除失败，系统原因";
            }
        }

        return $res;

    }
    public function updateGoodsById($goods_id,$update_array){
        $update_str = '';
        foreach($update_array as $filed=>$value){
            $update_str.= "$filed='$value',";
        }
        $update_str = rtrim($update_str,',');
        $sql = "update {$this->getTable()} set $update_str where good_id='$goods_id'";
        return $this->Update($sql);
    }
    public function goodsRestore($goods_id){
        $sql = "update {$this->getTable()} set good_status=1 where good_id='$goods_id'";
        return $this->Update($sql);
    }
    public function delGoodsReal($goods_id){
        $sql="delete from {$this->getTable()} where good_id='$goods_id'";
        return $this->Delete($sql);
    }
}